﻿using System.Diagnostics;
using UnityEngine;
using System.Collections;
using System.Reflection;
using System.IO;
using System.Threading;

public class CommandLineTool
{
    public static int ERROR_TIMEOUT = 1111111;
    public static int Cmd(string command, string arguments)
	{
		return CmdEx(command, arguments, "", false);
	}

    public static int CmdWithOutput(string command, string arguments, ref string output, ref string outputError)
	{
        return CmdExWithOutput(command, arguments, Application.dataPath + "/..", false, true, ref output, true, ref outputError);
	}

    public static int CmdEx(string command, string arguments, string workDir, bool isWin)
	{
		string output = null;
        string outputError = null;
        return CmdExWithOutput(command, arguments, Application.dataPath + "/../" + workDir, isWin, false, ref output, false, ref outputError);
	}

    public static int CmdExWithOutput(string command, string arguments, string workDir, bool isWin, bool isOutput, ref string output, bool isOutputError, ref string outputError)
    {
        int i = 0;
        while(i < 5)
        {
            i++;
            Process p = new Process();
            p.StartInfo.FileName=command;
            p.StartInfo.Arguments = arguments;
            p.StartInfo.WorkingDirectory = workDir;
            p.StartInfo.UseShellExecute = isWin;

            if(p.StartInfo.UseShellExecute)
            {
    //            p.StartInfo.RedirectStandardError=false;
    //            p.StartInfo.RedirectStandardOutput=false;
    //            p.StartInfo.RedirectStandardInput=false;
            }
            else
            {
                p.StartInfo.RedirectStandardError=true;
    			p.StartInfo.RedirectStandardOutput = true;

            }

            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.ErrorDialog = true;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            p.Start();
    		
    		if (isOutput)
    		{
                try
                {
        			System.Text.StringBuilder outputBuilder = new System.Text.StringBuilder();
        			while (!p.StandardOutput.EndOfStream)
        			{
        				string line = p.StandardOutput.ReadLine();
        				outputBuilder.AppendLine(line);
        			}

        			output = outputBuilder.ToString();
                }
                catch(System.Exception e)
                {
                    output = e.Message;
                }
    		}
    		else
    		{
    			if(!p.WaitForExit(10000))
                {
                    if(i >= 5)
                    {
                        return ERROR_TIMEOUT;
                    }

                    Thread.Sleep(2000);
                    continue;
                }
    		}

            try
            {
                outputError = p.StandardError.ReadToEnd();
            }
            catch(System.Exception e)
            {
                outputError = e.Message;
            }

            while (!p.HasExited)
            {
                continue;
            }
            return p.ExitCode;

        }

        return ERROR_TIMEOUT;
    }
}